<?php
/**
 * $Id: utils.class.php 417 2006-06-04 23:26:45Z dave $
 *
 * FGS is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */ 

class fgsUtils {

	function getIp() {
		$ip = null;
		if (getenv('HTTP_CLIENT_IP'))
			$ip = getenv('HTTP_CLIENT_IP');
		else if (getenv('HTTP_X_FORWARDED_FOR'))
			$ip = getenv('HTTP_X_FORWARDED_FOR');
		else if (getenv('REMOTE_ADDR'))
			$ip = getenv('REMOTE_ADDR');
		else
			$ip = 'UNKNOWN';
		return $ip;
	}

	function testFileWrite($name) {
		$fp = @fopen($name, 'w');
		if (!$fp)
			return false;
		fclose($fp);
		return true;
	}

	function formatDate($timestamp, $type = 0) {
		switch ($type) {
			case 1:
				return date(tr('__datemini__'), $timestamp);
			default:
				return date(tr('__date__'), $timestamp);
		}
	}

	function formatSeconds($seconds) {
		$minutes = 0;
		$hours = 0;
		$days = 0;
		if ($seconds >= 60) {
			$minutes = (int)($seconds / 60);
			$seconds -= $minutes * 60;
		}
		if ($minutes >= 60) {
			$hours = (int)($minutes / 60);
			$minutes -= $hours * 60;
		}
		if ($hours >= 24) {
			$days = (int)($hours / 24);
			$hours -= $days * 24;
		}

		$ret = array();
		if ($days) 
			$ret[] = sprintf(tr('%d days, '), $days);
		if ($hours)
			$ret[] = sprintf(tr('%dh'), $hours);
		if ($minutes)
			$ret[] = sprintf(tr('%dm'), $minutes);
		if ($seconds)
			$ret[] = sprintf(tr('%ds'), $seconds);
		else 
			$ret[] = '-';

		return implode('', $ret);
	}

	function formatField($msg, $value, $fallback = false) {
		if (isset($value)) {
			if ($msg)
				return sprintf('%s: %s', tr($msg), $value);
			else
				return $value;
		} else if ($fallback) {
			return tr($fallback);
		}
		return '';
	}

	function mtimef() {
		list($usec, $sec) = explode(' ', microtime());
		return ((float)$usec + (float)$sec);
	}


}

?>
